package com.example.controller;

import com.example.pojo.ContentCategory;
import com.example.pojo.ResultBean;
import com.example.req.ContentCategoryReq;
import com.example.service.ContentCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/contentCategory")
public class ContentCategoryController {
    @Autowired
    private ContentCategoryService contentCategoryService;

    @GetMapping("/queryAll")
    public ResultBean queryAll(){
        System.out.println("查詢全部廣告類別");
        List<ContentCategory> contentCategories = contentCategoryService.queryAll();
        return ResultBean.success(contentCategories);
    }

    @GetMapping("list")
    public ResultBean list(ContentCategoryReq contentCategoryReq){
        System.out.println("查詢的條件：" + contentCategoryReq);
        PageHelper.startPage(contentCategoryReq.getPageNum(),contentCategoryReq.getPageSize());
        List<ContentCategory> contentCategories = contentCategoryService.queryByCondition(contentCategoryReq);
        return ResultBean.success(new PageInfo(contentCategories));
    }

    @PostMapping("saveOrUpdate")
    public ResultBean saveOrUpdate(@RequestBody ContentCategory contentCategory){
        System.out.println("操作的數據：" + contentCategory);
        int rs = contentCategoryService.saveOrUpdate(contentCategory);
        return rs > 0 ? ResultBean.success() : ResultBean.error();
    }

    @DeleteMapping("deleteById/{id}")
    public ResultBean deleteById(@PathVariable("id") Long id){
        System.out.println("要刪除的Id" + id);
        int rs = contentCategoryService.deleteById(id);
        return rs > 0 ? ResultBean.success() : ResultBean.error();
    }
}
